package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.CsToken;
import com.ruoyi.system.mapper.CsTokenMapper;
import com.ruoyi.system.service.ICsTokenService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * CS Token 存储Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-14
 */
@Service
public class CsTokenServiceImpl extends ServiceImpl<CsTokenMapper, CsToken> implements ICsTokenService {
    @Autowired
    private CsTokenMapper csTokenMapper;

    /**
     * 查询CS Token 存储
     *
     * @param id CS Token 存储主键
     * @return CS Token 存储
     */
    @Override
    public CsToken selectCsTokenById(Long id) {
        return csTokenMapper.selectCsTokenById(id);
    }

    /**
     * 查询CS Token 存储列表
     *
     * @param csToken CS Token 存储
     * @return CS Token 存储
     */
    @Override
    public List<CsToken> selectCsTokenList(CsToken csToken) {
        return csTokenMapper.selectCsTokenList(csToken);
    }

    /**
     * 新增CS Token 存储
     *
     * @param csToken CS Token 存储
     * @return 结果
     */
    @Override
    public int insertCsToken(CsToken csToken) {
        return csTokenMapper.insertCsToken(csToken);
    }

    /**
     * 修改CS Token 存储
     *
     * @param csToken CS Token 存储
     * @return 结果
     */
    @Override
    public int updateCsToken(CsToken csToken) {
        return csTokenMapper.updateCsToken(csToken);
    }

    /**
     * 批量删除CS Token 存储
     *
     * @param ids 需要删除的CS Token 存储主键
     * @return 结果
     */
    @Override
    public int deleteCsTokenByIds(String ids) {
        return csTokenMapper.deleteCsTokenByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除CS Token 存储信息
     *
     * @param id CS Token 存储主键
     * @return 结果
     */
    @Override
    public int deleteCsTokenById(Long id) {
        return csTokenMapper.deleteCsTokenById(id);
    }

    @Override
    public CsToken selectCsTokenByTimestamp() {
        return csTokenMapper.selectOne(new LambdaQueryWrapper<CsToken>()
                .eq(CsToken::getStatus, 1)
                .orderByAsc(CsToken::getUpdateTimestamp)
                .last("limit 1"));
    }

    @Override
    public String selectSteamDtBearerTokenByTimestamp() {
        CsToken csToken = csTokenMapper.selectOne(new LambdaQueryWrapper<CsToken>()
                .eq(CsToken::getStatus, 1)
                .isNotNull(CsToken::getSteamDtBearerToken)
                .orderByAsc(CsToken::getUpdateTimestamp)
                .last("limit 1 for update"));
        if (ObjectUtils.isEmpty(csToken)) {
            return null;
        }
        csTokenMapper.update(null, new LambdaUpdateWrapper<CsToken>()
                .eq(CsToken::getId, csToken.getId())
                .set(CsToken::getUpdateTimestamp, System.currentTimeMillis()));
        return csToken.getSteamDtBearerToken();
    }
}
